package com.xxkj.drugtraceability.ui.doctoradvicecollection

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.databinding.LayoutDoctoradvicecollectionBinding
import com.xxkj.drugtraceability.databinding.LayoutDrugScan2Binding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class DoctorAdviceCollectionAdapter(private val actionClick:(index:Int)->Unit):BaseQuickAdapter<DrugInfoBean, BaseDataBindingHolder<LayoutDoctoradvicecollectionBinding>>(R.layout.layout_doctoradvicecollection) {
    private var scan = false
    fun setCanScan(scan:Boolean){
        this.scan = scan
    }
    override fun convert(
        holder: BaseDataBindingHolder<LayoutDoctoradvicecollectionBinding>,
        item: DrugInfoBean
    ) {
        try {
            holder.dataBinding?.apply {
                val buffer = StringBuffer()
                buffer.append("<font color='#33333F' size='50px'>")
                buffer.append("<b>"+item.drugName+"    "+"</b>")
                buffer.append("</font>")
                buffer.append("<font color='#494953' size='24px'>")
                buffer.append(item.drugSpec)
                buffer.append("</font>")
                if (item.trdnFlag == "1") {
                    //当前药品是否是拆零标识  1是 0否
                    try {
                        buffer.append("<img src='" + R.mipmap.icon_unpack + "'/>")
                        tvContent.text = Html.fromHtml(buffer.toString(), {
                            var drawable = ContextCompat.getDrawable(context, R.mipmap.icon_unpack)
                            drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                            drawable!!
                        }, null)
                    } catch (e: Exception) {
                        tvContent.text = Html.fromHtml(buffer.toString())
                    }
                } else {
                    tvContent.text = Html.fromHtml(buffer.toString())
                }
                tvRight.text = item.drugNum?.toDouble()?.toInt()?.toString()/*+item.drugRetailSoec*/
                if ("Y"==item.takeDrug){
                    tvType.text = "出"
                    tvType.setBackgroundResource(R.drawable.item_out)
                }else{
                    if ("1"==item.presType){
                        tvType.text = "长"
                        tvType.setBackgroundResource(R.drawable.item_long)
                    }else if ("2"==item.presType){
                        tvType.text = "临"
                        tvType.setBackgroundResource(R.drawable.item_short)
                    }
                }
                llAdd.removeAllViews()
                    //被选中
                 llParent.isSelected = item.isSelected
                llParent.setOnClickListener {
                    item.isSelected = !item.isSelected
                    if (item.isSelected){
                        actionClick.invoke(item.index)
                    }else{
                        actionClick.invoke(-1)
                    }
                    notifyDataSetChanged()
                }
                if (item.drugTracInfoList==null){
                    item.drugTracInfoList = mutableListOf()
                }
                if (item.drugTracInfoList.isNullOrEmpty()){
                    llBottom.visibility = View.GONE
                }else{
                    llBottom.visibility = View.VISIBLE
                    item.drugTracInfoList?.forEach { drugTrace->
                        val viewDrug =  LayoutInflater.from(context).inflate(R.layout.layout_drug_scan_2,null)
                        val bind = DataBindingUtil.bind<LayoutDrugScan2Binding>(viewDrug)
                        bind?.apply {
                            data =drugTrace.drugTracCodg
                            tvMore.visibility = View.GONE
                            ivDown.visibility = View.GONE
                            if (scan){
                                ivDelete.visibility = View.VISIBLE
                                ivDelete.tag = viewDrug
                                ivDelete.clickN {
                                    //删除
                                    item.drugTracInfoList?.remove(drugTrace)
                                    llAdd.removeView(ivDelete.tag as View)
                                }
                                if (item.trdnFlag == "1") {
                                    etDrugNum.visibility = View.VISIBLE
                                    if (drugTrace.drugTracCodeNum != null) {
                                        etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                                    }
                                    etDrugNum.doAfterTextChanged {
                                        try {
                                            drugTrace.drugTracCodeNum = Integer(it!!.toString())
                                        } catch (e: Exception) {
                                        }
                                    }
                                    etDrugNum.setOnFocusChangeListener { _, hasFocus ->
                                        if (!hasFocus && etDrugNum.text.isNullOrEmpty()) {
                                            drugTrace.drugTracCodeNum = Integer(1)
                                            etDrugNum.setText("1")
                                        }
                                    }
                                } else {
                                    drugTrace.drugTracCodeNum = Integer(1)
                                }
                            }else{
                                ivDelete.visibility = View.INVISIBLE
                                if (item.trdnFlag == "1" && drugTrace.drugTracCodeNum != null) {
                                    etDrugNum.visibility = View.VISIBLE
                                    etDrugNum.isEnabled = false
                                    etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                                }
                            }

                            llAdd.addView(viewDrug)
                        }
                    }

                }
            }
        } catch (e: Exception) {
        }

    }
}